package com.freight.core.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.freight.core.base.FreightBaseEntity;
import com.freight.core.common.constant.CommonConstant;
import com.freight.core.common.utils.NameUtil;
import com.freight.core.vo.PermissionDTO;
import com.freight.core.vo.RoleDTO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.annotations.ColumnTransformer;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Pattern;
import java.util.Date;
import java.util.List;

/**
 * @describe x
 */
@Data
@Accessors(chain = true)
@Entity
@DynamicInsert
@DynamicUpdate
@Table(name = "t_user")
@TableName("t_user")
@ApiModel(value = "用户")
public class User extends FreightBaseEntity {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "登录名")
    @Column(unique = true, nullable = false)
    @Pattern(regexp = NameUtil.regUsername, message = "登录账号不能包含特殊字符且长度不能>16")
    private String username;

    @ApiModelProperty(value = "用户类型 0托运人 1司机 2管理员")
    private Integer userType = CommonConstant.USER_TYPE_DRIVER;

    @ApiModelProperty(value = "所在省市")
    private String address;

    @ApiModelProperty(value = "街道地址")
    private String street;

//    @ApiModelProperty(value = "注册状态 0 临时 1 正常 2 审核中")
//    private Integer registStatus = CommonConstant.REGIST;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "姓名")
    private String nickName;

    @ApiModelProperty(value = "手机")
    @Pattern(regexp = NameUtil.regMobile, message = "11位手机号格式不正确")
    @ColumnTransformer(read = "AES_DECRYPT(UNHEX(mobile),'freight')", write = "HEX(AES_ENCRYPT(?, 'freight'))")
    private String mobile;

    @ApiModelProperty(value = "邮箱")
    private String email;

    @ApiModelProperty(value = "用户头像")
    private String avatar;

    @ApiModelProperty(value = "用户类型 0普通用户权限 1管理员")
    private Integer type = CommonConstant.USER_PRI_TYPE_NORMAL;

    @ApiModelProperty(value = "状态 默认0正常 -1拉黑")
    private Integer status = CommonConstant.USER_STATUS_NORMAL;

    @ApiModelProperty(value = "所属部门id")
    private Long departmentId;

    @ApiModelProperty(value = "所属部门名称")
    private String departmentTitle;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "生日")
    private Date birth;


    @Transient
    @TableField(exist = false)
    @ApiModelProperty(value = "用户拥有角色")
    private List<RoleDTO> roles;

    @Transient
    @TableField(exist = false)
    @ApiModelProperty(value = "用户拥有的权限")
    private List<PermissionDTO> permissions;

    @Transient
    @TableField(exist = false)
    @ApiModelProperty(value = "导入数据时使用")
    private Integer defaultRole;
}
